﻿using System;

namespace EnterpriseLibraryExtensions.Events
{
    /// <summary>
    /// This class encapsulates a delegate which can participate in the eventing system.
    /// </summary>
    /// <typeparam name="TEventType">The type of event the encapsulated action can handle.</typeparam>
    internal class InlineEvent<TEventType> : HandlesEvent<TEventType>
    {
        /// <summary>
        /// The Action delegate to invoke when the event of type TEventType is raised.
        /// </summary>
        private Action<object, EventArgs<TEventType>> _action;

        /// <summary>
        /// Initializes a new instance of the InlineEvent class.
        /// The constructor takes the action that will be invoked when the event of type TEventType is raised.
        /// </summary>
        /// <param name="action">The action to invoke on event. The value cannot be null.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if action is null.</exception>
        public InlineEvent(Action<object, EventArgs<TEventType>> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            _action = action;
        }

        /// <summary>
        /// Called by the eventing framework when the inline event must call the action.
        /// </summary>
        /// <param name="sender">The instance raising the event or null.</param>
        /// <param name="args">The arguments associated with the event.</param>
        protected override void Handle(object sender, EventArgs<TEventType> args)
        {
            _action(sender, args);
        }
    }
}
